package utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 数据库连接和断开
 * @author 32941
 *
 */

public class JDBCUtil {
	
    /**
     * 获取jdbc连接对象,连接数据库
     * @return
     */
	public static Connection getConnection() {
		Connection conn=null;
		try {
			DruidDataSource dataSource = DSUtil.getDataSource();
			conn=dataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
	}
	/**
     * 关闭连接对象
     * @param rs
     * @param stmt
     * @param conn
     */
	public static void closeConnection(ResultSet result,PreparedStatement preparedStatement,Connection connection) {
		try {
			if(result!=null) {
				result.close();
			}
			if(preparedStatement!=null) {
				preparedStatement.close();
			}
			if(connection!=null) {
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
